package org.jeecg.modules.anodeChange.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.anodeChange.entity.AnodeChange;
import org.jeecg.modules.anodeChange.entity.AnodeChangeRecord;
import org.jeecg.modules.anodeChange.service.IAnodeChangeRecordService;
import org.jeecg.modules.anodeChange.service.IAnodeChangeService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.userFormula.entity.UserFormulaDetail;
import org.jeecg.modules.userFormula.entity.UserFormulaPortion;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: 换极设置
 * @Author: jeecg-boot
 * @Date: 2023-10-12
 * @Version: V1.0
 */
@Api(tags = "换极设置")
@RestController
@RequestMapping("/anodeChange/anodeChange")
@Slf4j
public class AnodeChangeController extends JeecgController<AnodeChange, IAnodeChangeService> {
    @Autowired
    private IAnodeChangeService anodeChangeService;
    @Autowired
    private IAnodeChangeRecordService anodeChangeRecordService;

    /**
     * 分页列表查询
     *
     * @param anodeChange
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "换极设置-分页列表查询")
    @ApiOperation(value = "换极设置-分页列表查询", notes = "换极设置-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<AnodeChange>> queryPageList(AnodeChange anodeChange,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<AnodeChange> queryWrapper = QueryGenerator.initQueryWrapper(anodeChange, req.getParameterMap());
        Page<AnodeChange> page = new Page<AnodeChange>(pageNo, pageSize);
        IPage<AnodeChange> pageList = anodeChangeService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param anodeChange
     * @return
     */
    @AutoLog(value = "换极设置-添加")
    @ApiOperation(value = "换极设置-添加", notes = "换极设置-添加")
    //@RequiresPermissions("anodeChange:tb_anode_change:add")
    @PostMapping(value = "/add")
    @Transactional
    public Result<String> add(@RequestBody AnodeChange anodeChange) {
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.DATE, anodeChange.getTimeLimit());
        Date date2 = calendar.getTime();
        anodeChange.setChangeDate(date2);
        anodeChangeService.save(anodeChange);

        LambdaQueryWrapper<AnodeChangeRecord> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(AnodeChangeRecord::getSlotName, anodeChange.getSlotName());
		queryWrapper.eq(AnodeChangeRecord::getPosition, anodeChange.getPosition());
        anodeChangeRecordService.remove(queryWrapper);
        AnodeChangeRecord anodeChangeRecord = new AnodeChangeRecord();
        BeanUtils.copyProperties(anodeChange,anodeChangeRecord);
        anodeChangeRecord.setAnodeChangeId(anodeChange.getId());
        anodeChangeRecord.setStatus(0);
        anodeChangeRecordService.save(anodeChangeRecord);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param anodeChange
     * @return
     */
    @AutoLog(value = "换极设置-编辑")
    @ApiOperation(value = "换极设置-编辑", notes = "换极设置-编辑")
    //@RequiresPermissions("anodeChange:tb_anode_change:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody AnodeChange anodeChange) {
        anodeChangeService.updateById(anodeChange);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "换极设置-通过id删除")
    @ApiOperation(value = "换极设置-通过id删除", notes = "换极设置-通过id删除")
    //@RequiresPermissions("anodeChange:tb_anode_change:delete")
    @DeleteMapping(value = "/delete")
    @Transactional
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        anodeChangeService.removeById(id);
        LambdaQueryWrapper<AnodeChangeRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AnodeChangeRecord::getAnodeChangeId, id);
        anodeChangeRecordService.remove(queryWrapper);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "换极设置-批量删除")
    @ApiOperation(value = "换极设置-批量删除", notes = "换极设置-批量删除")
    //@RequiresPermissions("anodeChange:tb_anode_change:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    @Transactional
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.anodeChangeService.removeByIds(Arrays.asList(ids.split(",")));
        LambdaQueryWrapper<AnodeChangeRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AnodeChangeRecord::getAnodeChangeId, Arrays.asList(ids.split(",")));
        anodeChangeRecordService.remove(queryWrapper);
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "换极设置-通过id查询")
    @ApiOperation(value = "换极设置-通过id查询", notes = "换极设置-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<AnodeChange> queryById(@RequestParam(name = "id", required = true) String id) {
        AnodeChange anodeChange = anodeChangeService.getById(id);
        if (anodeChange == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(anodeChange);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param anodeChange
     */
    //@RequiresPermissions("anodeChange:tb_anode_change:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AnodeChange anodeChange) {
        return super.exportXls(request, anodeChange, AnodeChange.class, "换极设置");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("anodeChange:tb_anode_change:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AnodeChange.class);
    }

}
